﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.Data;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;

namespace DCSLibrary
{
    public class ProductCommentUI
    {
        private const String CACHE_KEY = "ProductComment_GetAll";

        public int Add(int ProductId, int UserID, String Content, int Rate, String CommentDate, byte Status, String IP)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "ProductComment_add", ProductId, UserID, Content, Rate, CommentDate, Status, IP);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;

            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int ProductCommentID, int ProductId, int UserID, String Content, int Rate, String CommentDate, byte Status, String IP)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "ProductComment_update", ProductCommentID, ProductId, UserID, Content, Rate, CommentDate, Status, IP);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + ProductCommentID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public DataSet GetAll()
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "ProductComment_getall");
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByID(int ProductCommentID)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + ProductCommentID.ToString());
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "ProductComment_get", ProductCommentID);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + ProductCommentID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public int DeleteByID(int ProductCommentID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "ProductComment_delete", ProductCommentID);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception e)
            {
                return -1;
            }
        }
    }
}
